package com.csproj.basic.resource;

import com.codahale.metrics.annotation.Timed;
import com.csproj.basic.service.ElectronicLockMessageService;
import com.csproj.basic.service.dto.ElectronicLockMessageDTO;
import com.csproj.siiat.base.common.dto.BaseDTO;
import com.csproj.system.domain.UserInfo;
import com.csproj.system.service.SysLogsService;
import com.csproj.system.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

/**
 * REST controller for managing ElectronicLockMessage.
 */
@RestController
@RequestMapping("/api/siiat/v1")
public class ElectronicLockMessageResource {

    private final Logger log = LoggerFactory.getLogger(ElectronicLockMessageResource.class);

    private static final String ENTITY_NAME = "electronicLockMessage";

    private final ElectronicLockMessageService electronicLockMessageService;
    private final UserInfoService userInfoService;
    private final SysLogsService sysLogsService;
    public ElectronicLockMessageResource(ElectronicLockMessageService electronicLockMessageService, UserInfoService userInfoService, SysLogsService sysLogsService) {
        this.electronicLockMessageService = electronicLockMessageService;
        this.userInfoService = userInfoService;
        this.sysLogsService = sysLogsService;
    }
    /**
     * GET  /electroniclockmessage: 获取电子关锁信息(不分页)
     * @return BaseDTO
     */
    @GetMapping("/allelectroniclockmessage")
    @Timed
    public BaseDTO getAllElectronicLockMessages() {
        return BaseDTO.createBaseDTO().setFieldList(electronicLockMessageService.findAll());
    }

    /**
     * GET  /electroniclock/:id : 根据设备id，获取读取设备信息
     *
     * @param id id
     * @return pda信息
     */
    @GetMapping("/electroniclockMessage/{id}")
    @Timed
    public BaseDTO getElectronicLockMessage(@PathVariable String id) {
        ElectronicLockMessageDTO dto = electronicLockMessageService.findOneAndGetDTO(id);
        return BaseDTO.createBaseDTO().setData(dto);
    }

    @GetMapping("/electroniclockMessage/getLastAddress")
    @Timed
    public  BaseDTO getLastAddressByLockNo(@RequestParam(value = "lockNo",required = false) String lockNo){
        UserInfo userInfo = userInfoService.getCurrentUser();
        sysLogsService.saveForGet("", userInfo.getName() + ":user_view_dispatch_online", "");
        ElectronicLockMessageDTO message = electronicLockMessageService.findFirstByElNumberOrderByTimeStringDesc(lockNo);
        return BaseDTO.createBaseDTO().setData(message);
    }

}
